package com.hujing.springsource.a12;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

//cglib代理：代理对象是子类，目标对象是父类
public class CglibProxyDemo {

    static class Target{
        public void foo(){
            System.out.println("target foo");
        }
    }

    public static void main(String[] args) {
        // 目标对象
        Target target = new Target();
        //创建代理对象
        Target proxy = (Target) Enhancer.create(Target.class, (MethodInterceptor) (p, method, args1, methodProxy) -> {
            System.out.println("before");
            Object result = method.invoke(target, args1);
            System.out.println("after");
            return result;
        });

        proxy.foo();
    }
}
